﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Ionic.Zip;

namespace ShadowEraDeckEditor
{
    class Settings
    {
        public string appVersion { get; set; }
        public string datVersion { get; set; }
        public string lastFolder { get; set; }
        public bool autoUpdate { get; set; }

        public Settings()
        {
            appVersion = "0.54";

            //read in the 3 lines in settings and apply them
            if (File.Exists("settings.ini"))
            {
                const string f = "settings.ini";
                List<string> settinglines = new List<string>();
                using (StreamReader r = new StreamReader(f))
                {
                    lastFolder = r.ReadLine();
                    string austring = r.ReadLine();
                    if (austring == "false")
                        autoUpdate = false;
                    else
                        autoUpdate = true;
                }
                if (!Directory.Exists(lastFolder))
                    lastFolder = Path.GetDirectoryName(Application.ExecutablePath);
            }
            //if settings.ini doesn't exist, create it
            else
            {
                using (StreamWriter writer = new StreamWriter("settings.ini"))
                {
                    writer.WriteLine(Path.GetDirectoryName(Application.ExecutablePath) + "\\");
                    lastFolder = Path.GetDirectoryName(Application.ExecutablePath);
                    writer.Write("true");
                    autoUpdate = true;
                }
            }
            if (File.Exists("carddb.dat"))
            {
                ZipFile zip = ZipFile.Read("carddb.dat");
                zip.Password = "c@n7cr4kD1zSh!t";
                ZipEntry z = zip["carddb.version.ini"];
                MemoryStream datver = new MemoryStream();
                z.Extract(datver);
                datver.Position = 0;
                using (StreamReader reader = new StreamReader(datver))
                {
                    datVersion = reader.ReadLine();
                    reader.Dispose();
                }
                datver.Dispose();
                zip.Dispose();
            }
            else
            {
                //MessageBox.Show("You are missing the program file \"carddb.dat\"");
                datVersion = "exit";
            }
        }

        public void WriteFolder(string s)
        {
            //read in the 3 lines in settings and apply them
            string austring = "";
            if (File.Exists("settings.ini"))
            {
                const string f = "settings.ini";
                List<string> settinglines = new List<string>();
                using (StreamReader r = new StreamReader(f))
                {
                    lastFolder = r.ReadLine();
                    austring = r.ReadLine();
                }
            }
            lastFolder = s.Substring(0, (s.LastIndexOf('\\') + 1));
            File.Delete("settings.ini");
            using (StreamWriter writer = new StreamWriter("settings.ini"))
            {
                writer.WriteLine(lastFolder);
                writer.Write(austring);
            }
        }

        public void WriteUpdate(string s)
        {
            //read in the 3 lines in settings and apply them
            if (File.Exists("settings.ini"))
            {
                const string f = "settings.ini";
                List<string> settinglines = new List<string>();
                using (StreamReader r = new StreamReader(f))
                {
                    lastFolder = r.ReadLine();
                }
                if (!Directory.Exists(lastFolder))
                    lastFolder = Path.GetDirectoryName(Application.ExecutablePath);
            }
            File.Delete("settings.ini");
            using (StreamWriter writer = new StreamWriter("settings.ini"))
            {
                writer.WriteLine(lastFolder);
                writer.Write(s);
            }
        }
    }
}